/*
 * ConnectionEditorPanel.java
 *
 * Created on November 15, 2008, 1:07 PM
 */
package com.borderbears.backend.gui;

import java.net.MalformedURLException;
import java.net.URL;

import com.borderbears.backend.connection.Connection;
import com.borderbears.backend.connection.RemoteXMLConnection;

/**
 * @author bmod
 */
public class ConnectionEditorForm extends ValidatingPanel {

	private Connection connection;

	/** Creates new form ConnectionEditorPanel */
	public ConnectionEditorForm() {
		initComponents();
	}

	public void setConnection(final Connection c) {
		connection = c;
	}

	public Connection getConnection() {
		return connection;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		tfName = new javax.swing.JTextField();
		tfURL = new javax.swing.JTextField();
		tfStatus = new javax.swing.JLabel();

		jLabel1.setText("Name:");
		jLabel1.setName("jLabel1"); // NOI18N

		jLabel2.setText("URL:");
		jLabel2.setName("jLabel2"); // NOI18N

		tfName.setName("tfName"); // NOI18N

		tfURL.setName("tfURL"); // NOI18N

		tfStatus.setText("Fill in a name and url to connect to.");
		tfStatus.setName("tfStatus"); // NOI18N

		final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel1)
														.addComponent(jLabel2))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																tfURL,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																331,
																Short.MAX_VALUE)
														.addComponent(
																tfName,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																331,
																Short.MAX_VALUE)
														.addComponent(
																tfStatus,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																331,
																Short.MAX_VALUE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																tfName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																tfURL,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(tfStatus)
										.addContainerGap(214, Short.MAX_VALUE)));
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField tfName;
	private javax.swing.JLabel tfStatus;
	private javax.swing.JTextField tfURL;

	// End of variables declaration//GEN-END:variables
	@Override
	public boolean isValid() {

		try {
			final String name = tfName.getText();
			if (name.trim().length() < 1) {
				tfStatus.setText("Name cannot be empty!");
				tfStatus.requestFocus();
				return false;
			}
			final URL url = new URL(tfURL.getText());

			connection = new RemoteXMLConnection(name, tfURL.getText());

			return true;
		} catch (final MalformedURLException ex) {
			tfStatus.setText("URL is not valid!");
			return false;
		}

	}
}
